import { WeaSearchGroup, WeaFormItem, WeaUpload, WeaTools } from 'ecCom';
import { Button, Form } from 'antd';
import { toJS } from 'mobx';
import { WeaSwitch } from 'comsMobx';
import { inject, observer } from 'mobx-react';

const getKey = WeaTools.getKey;

@observer
class AddForm extends React.Component {
  render() {
    const { customerAddForm, addForm } = this.props.listStore;
    const { isFormInit } = addForm;
    const group = [];
    const formParams = addForm.getFormParams();
    isFormInit && toJS(customerAddForm).map((c,indexGroup) => {
      const items = [];
      let dom;
      c.items.map((fields, index) => {
        if (fields.conditionType == 'UPLOAD') {
          const key = getKey(fields);
          const bindObj = addForm.$(key);
          dom = (<WeaUpload
            ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@jsby2n@${index}`}
            {...bindObj.bind()}
            {...fields}
            {...fields.otherParams}
            uploadId={fields.domkey[0]}
            uploadUrl="/api/crm/common/fileUpload"
            category="0,0,0"
            autoUpload
            showBatchLoad={false}
            showClearAll={false}
            multiSelection
            listType={fields.listType}
            onChange={(ids, lists) => { addForm.updateFields({ [fields.domkey[0]]: { value: `${ids}` } }, false); }}
          />);
        } else {
          dom = <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@l7vtsc@${index}`} fieldConfig={fields} form={addForm} />;
        }
        items.push({
          com: (<WeaFormItem
            ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@us5qhj@${index}`}
            label={`${fields.label}`}
            labelCol={{ span: `${fields.labelcol}` }}
            error={addForm.getError(fields)}
            tipPosition="bottom"
            wrapperCol={{ span: `${fields.fieldcol}` }}
          >
            {dom}
          </WeaFormItem>),
          colSpan: 1,
        });
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@id6u4o@${indexGroup}`} needTigger title={c.title} showGroup={c.defaultshow} items={items} />);
    });
    return group;
  }
}


export default AddForm;
